﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace FirstLimitUpMode.Handles
{
    public class SettingConfine
    {
        public const string MIN_PRICE = "最小价格";
        public const string MAX_PRICE = "最大价格";
        public const string MIN_MARKUP = "最小涨幅";
        public const string SELECT_LOCK = "固定自选";
        public const string TIME_LOCK = "固定时间";
        public const string P20_SHOW = "显示20";
        public const string INTERVAL_TOP = "涨停间隔";
        public const string BUYING_MARKUP = "下单涨幅";
        public const string GPAPP_PATH = "交易软件";
        public const string REFRESH_FREQUENCY = "刷新频率";
        public const string BUYIN_VALUE = "交易金额";
        public const string IS_TOP = "是否涨停下单";
        public const string MIN_VALUE = "市值限制";

    }
    public class SettingsHelper
    {
        private static string _fileName = "Settings.ini";
        private static Dictionary<string, string> _dic = new Dictionary<string, string>();
        public static bool Initialize()
        {
            try
            {
                if (!File.Exists(_fileName)) return false;

                var strs = File.ReadAllLines(_fileName);
                if(strs == null || strs.Length == 0) return false;
                foreach (var str in strs)
                {
                    var spStrs = str.Split('=');
                    if (_dic.ContainsKey(spStrs[0]))
                    {
                        _dic[spStrs[0]] = spStrs[1];
                    }
                    else
                    {
                        _dic.Add(spStrs[0], spStrs[1]);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Setting异常");
                return false;
            }
            return true;
        }
        public static void SaveSettings()
        {
            try
            {
                if(_dic.Count <= 0) return;
                StringBuilder stringBuilder = new StringBuilder();
                foreach (var item in _dic)
                {
                    stringBuilder.Append(item.Key);
                    stringBuilder.Append("=");
                    stringBuilder.Append(string.IsNullOrEmpty(item.Value)?" ":item.Value);
                    stringBuilder.Append("\n");
                }
                File.WriteAllText(_fileName, stringBuilder.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Setting保存异常");
            }
        }
        public static void SetValue(string name,object value)
        {
            if (_dic.ContainsKey(name))
            {
                _dic[name] = value.ToString();
            }
            else
            {
                _dic.Add(name, value.ToString());
            }
        }
        public static int GetIntByName(string name)
        {
            int res = 0;
            if (_dic.ContainsKey(name))
            {
                int.TryParse(_dic[name], out res); 
            }
            return res;
        }
        public static double GetDoubleByName(string name)
        {
            double res = 0.0;
            if (_dic.ContainsKey(name))
            {
                double.TryParse(_dic[name], out res);
            }
            return res;
        }
        public static float GetFloatByName(string name)
        {
            float res = 0.0f;
            if (_dic.ContainsKey(name))
            {
                float.TryParse(_dic[name], out res);
            }
            return res;
        }
        public static bool GetBoolByName(string name)
        {
            bool res = false;
            if (_dic.ContainsKey(name))
            {
                bool.TryParse(_dic[name], out res);
            }
            return res;
        }
        public static string GetStringByName(string name)
        {
            string res = "";
            if (_dic.ContainsKey(name))
            {
                res = _dic[name];
            }
            return res;
        }
    }
}
